﻿using cfg.Myskill;
using Events.EventsInfo;
using Unity.VisualScripting;
using UnityEngine;

public class ThrowSwordSkill:CloneBaseSkill
{
    private int dotNum = 20;
    private float dotDistance = 0.07f;
    //private float  swordGravity = 3;
    private GameObject[] dots;
    private Vector2 aimDir;
    public bool hasSword;//是否有剑
    public bool canReturn;//是否可以还剑
    
    public ThrowSword info;
    public ThrowSwordSkill(string skillName) : base(skillName)
    {
        hasSword = true;
        //注册事件监听
        EventCenter.Instance.AddEventListener<ReturnSwordInfo>(E_EventType.Sword_FinishReturn, ReturnSword);
        EventCenter.Instance.AddEventListener(E_EventType.Sword_CanReturn,CanReturn);
    }

    private void CanReturn()
    {
        canReturn =  true;
    }
    //归还剑
    private void ReturnSword(ReturnSwordInfo info)
    {
        hasSword = true;
        canReturn = false;
    }
    public override CloneController Clone(string prefab,Vector2 pos,int faceDir)
    {
        var  cloneController = base.Clone(prefab,pos,faceDir);
        var  swordController =  cloneController as SwordCloneController;
        if (swordController != null)
        {
            if (info==null)
            {
                info  = (LoadCofigManager.Instance.GetSkillInfo(skillName).Info) as  ThrowSword;
            }
            if (skillName.Equals(Setting.SkillBounceSword))//弹跳剑
            {
                swordController.CanBounce();//允许弹跳
                swordController.Play("sword_flip");
            }else if (skillName.Equals( Setting.SkillPierceSword))//穿透剑
            {
                swordController.CanPierce();//允许穿透
                swordController.Play("sword_idle");
            }else if (skillName.Equals(Setting.SkillRotateSword))//弹跳剑
            {
                swordController.CanRotate();//允许原地旋转
                swordController.Play("sword_flip");
            }
            else if (skillName.Equals(Setting.SkillThrowSword))
            {
                
                swordController.Play("sword_flip");
            }
            //swordController.Play("sword_flip");
            swordController.AddForce(aimDir);
        }
        hasSword = false;//无剑
        canReturn = false;//不能还剑
        SetDotsActive(false);
        return cloneController;
    }

    public override void Update()
    {
        base.Update();
        /*
        if (Input.GetKey(KeyCode.Q)&&hasSword)
        {
            //设置瞄准点位置
            GetAimDir();
            SetDots();
            SetDotsActive(true);
        }
        */

        //TODO:关于瞄准的部分应该写在AimState里面，skill只关心数据逻辑，不关心表现层逻辑，放在这里后续也不改了
        if (Input.GetKeyUp(KeyCode.Q))
        {
            //关闭瞄准点
            SetDotsActive(false);
        }
        if (PlayerManager.Instance.controller!=null)
        {
            if (PlayerManager.Instance.controller.stateMachine.EqualCurState(Setting.swordAim))//在瞄准状态
            {
                if (Input.GetKey(KeyCode.Q)&&hasSword)
                {
                    //设置瞄准点位置
                    GetAimDir();
                    SetDots();
                    SetDotsActive(true);
                }
            }
        }
        
    }

    private void SetDotsActive(bool active)
    {
        if (dots==null||dots.Length==0)
            return;
        foreach (var gameObject in dots)
        {
            gameObject.SetActive(active);
        }
    }
    private void SetDots()
    {
        if (dots==null)
        {
            dots = new GameObject[dotNum];
            GenerateDots();
        }
        var player = PlayerManager.Instance.controller;
        if (player!=null)
        {
            for (var i = 0; i < dots.Length; i++)
            {
                dots[i].transform.position = (Vector2)player.transform.position+GetDotPos(i * dotDistance);
            }
            
        }
    }
    
    private Vector2 GetDotPos(float t)
    {
        if (info==null)
        {
            info  = (LoadCofigManager.Instance.GetSkillInfo(skillName).Info) as  ThrowSword;
        }
        //公式 .5f * swordGravity * (t * t) 计算了在时间 t 内由于重力作用产生的垂直位移（自由落体运动公式）。
        var pos = new  Vector2(aimDir.x*info.ThrowForce,
            aimDir.y*info.ThrowForce*1.5f)*t +Physics2D.gravity * (.5f * info.SwordGravity * (t*t));
        return pos;
    }

    private void GetAimDir()
    {
        var player = PlayerManager.Instance.controller;
        if (Camera.main!=null&& player!=null)
        {
            aimDir = (Camera.main.ScreenToWorldPoint(Input.mousePosition)
                     - player.transform.position).normalized;
        }
    }

    private void GenerateDots()
    {
        for (int i = 0; i < dotNum; i++)
        {
            dots[i] = PoolMgr.Instance.GetObj("Dot");
        }
    }

    public override void OnDestroy()
    {
        base.OnDestroy();
        //回收
        if (dots!=null&&dots.Length>0)
        {
            foreach (var gameObject in dots)
            {
                PoolMgr.Instance.PushObj(gameObject);
            }
            
        }
        
        EventCenter.Instance.RemoveEventListener<ReturnSwordInfo>(E_EventType.Sword_FinishReturn,ReturnSword);
        EventCenter.Instance.RemoveEventListener(E_EventType.Sword_CanReturn,CanReturn);
    }
}